package bgate.hung.framework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

public class Tool {

	public static String readTextFile(InputStream in) throws IOException {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[4096];
		int len = 0;
		while((len = in.read(bytes)) > 0)
				byteStream.write(bytes, 0, len);
		return new String(byteStream.toByteArray(), "UTF-8");
	}
	
	public static String readTextLine(InputStream in) throws IOException{
        int ch = in.read();
        if (ch == -1) {
            in.close();
            return null;
        } else {
            String temp = "";
            while ((ch != -1) && (ch != 13)) {
                if (ch != 10) {
                    temp += (char) ch;
                }
                ch = in.read();
            }
            return temp;
        }
    }
	
	public static void writeTextFile(File file, String text) throws IOException {
		BufferedWriter write = new BufferedWriter(new FileWriter(file));
		write.write(text);
		write.close();
	}
	
	public static String readTextLine(File file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		StringBuffer text = new StringBuffer();
		String line;
		while ((line = reader.readLine()) != null) {
			text.append(line);
			text.append("\n");
		}
		reader.close();
		return text.toString();
	}
 }
